<!--
 * @Author: hq
 * @Date: 2022-05-28 19:29:52
 * @LastEditors: hq
 * @LastEditTime: 2022-06-30 17:13:25
 * @Description: 添加分类弹窗
 * @version: 1.0
-->
<template>
  <div class="change">
    <el-row>
      <el-col :span="12">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="分类名称" prop="name">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入分类名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类图标" :required="true">
            <el-upload
              v-loading="loading"
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :http-request="upLoadServe"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
            <el-input
              maxlength="3"
              v-model="ruleForm.sort"
              placeholder="请输入排序"
            ></el-input>
          </el-form-item>
          <el-form-item label="状态" :required="true">
            <el-switch
              active-text="禁用"
              inactive-text="启用"
              :active-value="1"
              :inactive-value="0"
              class="tablescope"
              v-model="ruleForm.status"
            >
            </el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >确定</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { editClassification } from "@/api/mall_management";
import upImg from "@/utils/upimg";
export default {
  props: ["rowData"],
  data() {
    return {
      loading: false,
      rowId: "",
      ruleForm: {
        name: "",
        sort: "",
        status: 1,
      },
      imageUrl: "",
      imageUrlId: "",
      rules: {
        name: [{ required: true, message: "请输入分类名称", trigger: "blur" }],
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
      },
    };
  },
  methods: {
    async editClassification() {
      const { data, err } = await editClassification({
        id: this.rowId,
        name: this.ruleForm.name,
        icon: this.imageUrlId,
        sort: this.ruleForm.sort,
        status: this.ruleForm.status,
        sid: 7,
      });
      if (err === 0) {
        if (this.rowId === "") {
          this.$message({
            showClose: true,
            message: "新增成功",
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: "编辑成功",
            type: "success",
          });
        }
        this.$emit("handleClose");
      }
    },
    //select改变
    changeSelect() {
      this.$forceUpdate();
    },
    upLoadServe(file) {
      this.loading = true;
      upImg.upimg(
        (data) => {
          console.log(data);
          this.imageUrl = data.data.pre_img;
          console.log(this.imageUrl);
          this.imageUrlId = data.data.url;
          this.loading = false;
        },
        1,
        file
      );
      //   let formData = new FormData();
      //   formData.append("file", file.file);
      //   this.upLoad(formData);
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //接口
          this.editClassification();
        }
      });
    },
  },
  created() {
    if (this.rowData.id) {
      //编辑
      this.ruleForm = this.rowData;
      this.imageUrlId = this.rowData.icon.at_id;
      this.rowData.icon.src;
      this.imageUrl = this.rowData.icon.src;
      this.rowId = this.rowData.id;
      //   this.upClassifyArr = this.upClassifyArr.concat(this.rowData.children);
    }
  },
};
</script>

<style lang="scss" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader {
  width: 82px;
  height: 82px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
